import dataList from '@/components/dataList'
import {fetchList, getLessorContractPageList, updateLessorPayableByCommit, isAdjustment, changeAdjustment} from "../../api/lessor-payable"
import editData from '../edit/edit-data'
import editAdjustment from '../edit/edit-adjustment'
import showActual from "../edit/show-actual"
import contractView from "@/views/wy-lessor/rentalSociety/views/contract/view.vue"
export default {
    name: "index",
    components: {
        dataList,
        contractView,
        editData,
        editAdjustment,
        showActual
    },
    provide() {
        return {
            // 更新核算列表
            initList: this.getListData,
            // 更新合同列表
            updateList: this.getContractList,
        }
    },
    beforeMount() {
        this.getContractList()
    },
    data() {
        return {
            contract: {
                // 列表数据
                tableData: [],
                // 请求参数
                params: {
                    current: 1,
                    size: 10,
                    projectName: ''
                },
                // 分页
                pager: {
                    total: 0, // 总页数
                    current: 1, // 当前页数
                    size: 10 // 每页显示多少条
                },
                // 页面loading
                loading: false,
            },
            tableData: [],
            // 请求参数
            params: {
                current: 1,
                size: 10,
                projectName: ''
            },
            // 选中的数据
            selectionList: [],
            // 分页
            pager: {
                total: 0, // 总页数
                current: 1, // 当前页数
                size: 10 // 每页显示多少条
            },
            // 页面loading
            loading: false,
						showPayable: false,
						// 初始化显示状态识别符
						flag: false,
            filterList: [
                { label: '付款项目', inputType: 'dict', dictName: "charge_project_type_lessor", name: 'chargeProjectName', multiple: true},
                { label: '费用周期', inputType: 'date', name: 'costPeriodStart'},
                { label: '状态', inputType: 'dict', dictName: 'payable_status', name: 'payableStatus', multiple: true}
            ],
            NewIcon: require('@/assets/images/new.png'),
            editDataShow: false
        }
    },
    methods: {
        // 获取合同列表
        getContractList(params = {}) {
            this.contract.loading = true
            let requestData = {...this.contract.params, ...params}
            getLessorContractPageList(requestData).then(resp => {
                const { current, size, total, records } = resp.data.data
                this.contract.tableData = records.map((item, index) => ({...item, active: index === 0}))
                this.contract.params = requestData
                this.contract.pager = { total, current, size }
								if (this.contract.tableData.length !== 0) {
									this.changeAccountList(this.contract.tableData[0].lessorContractId)
								} else {
									this.changeAccountList(0)
								}
            }).catch(() => {
                this.tableLoading = false
            }).finally(() => {
                this.contract.loading = false
            })
        },
        //切换
        changeAccountList(lessorContractId) {
            this.contract.tableData = this.contract.tableData.map(item => ({...item, active: item.lessorContractId == lessorContractId}))
            if (lessorContractId === undefined || lessorContractId === null || lessorContractId === '') {
                if (this.contract.tableData.length > 0) {
                    this.params.lessorContractId = lessorContractId
										this.$refs.dataList.setFilter(new Map([['payableStatus',['10']]]))
                    this.getListData({current: 1, payableStatus: '10'})
                }
            } else {
                this.params.lessorContractId = lessorContractId
								this.$refs.dataList.setFilter(new Map([['payableStatus',['10']]]))
                this.getListData({current: 1, payableStatus: '10'})
            }
        },
        getListData(params = {}) {
            this.loading = true
						if (!this.flag) params.payableStatus = "10";
            let requestData = {...this.params, ...params}
            fetchList(requestData).then(res => {
								this.flag = true;
                const  {current, size, total, records} = res.data.data
                this.tableData = records
                this.params = requestData
                this.pager = {total, current, size}
            }).finally(() => {
                this.loading = false
            })
        },
        //查看
        handleShowReceivableData(row) {
        	this.showPayable = true
					this.$nextTick(() => {
						this.$refs.showActual.show(row)
					})
        },
        //提交
        handleClickSubmit(row) {
            this.$confirm('是否确认提交?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                updateLessorPayableByCommit(row.payableId).then(res => {
                    if (res.data.code === 0 && res.data.data === true) {
                        this.$message.success('提交成功！')
                        this.getListData()
                    } else {
                        this.$message.error('提交失败！')
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消提交'
                })
            })
        },
        //应付调整
        handleEditReceivable() {
            if (this.selectionList.length == 1) {
                if(this.selectionList.find(item=>item.payableStatus === "01")){
                    this.selectionList.map(item => {
                        isAdjustment(item.payableId).then(res => {
                            if (res.data.code === 0 && res.data.msg === '1') {
                                changeAdjustment(item.payableId).then(rep => {
                                    if (rep.data.code === 0) {
                                        this.editDataShow = true
                                        this.$nextTick(() => {
                                            this.$refs.editAdjustment.show(this.selectionList[0])
                                        })
                                    } else {
                                        this.err(rep.data.msg)
                                        return
                                    }
                                })
                            } else {
                                this.err('当前数据已在应付调整审批中！')
                                return
                            }
                        })
                    })
                }else {
                    this.err('仅可调整未支付状态下数据！')
                    return
                }
            }else{
                this.err('请选择一个要调整的项目')
            }
        },
        //导出
        downloadData(){
            this.publicExport('应付','/api/lessorpayable/downloadExcel',this.params)
        },
        handleClickEditData(item) {
            this.editDataShow = true
            this.$nextTick(() => {
                this.$refs.editData.show(item)
            })
        },
        handleClose() {
            this.editDataShow = false
        },
        // 菜单点击监听
        handleMenuClick(type){
            switch (type) {
                case 'test':
                    this.handleEditReceivable();
                    break;
                case 'export':
                    this.downloadData();
                    break;
                default:
                    this.$message(type)
                    break;
            }
        },
			//保存后销毁
			resetList() {
				this.editDataShow = false
				this.getListData()
			},
			// 查看销毁
			handleShowClose() {
				this.showPayable = false
			},
    },
    filters: {
        ellipsis(value) {
            if (!value) {
                return ''
            } else if (value.length > 8) {
                return value.slice(0,8) + '...'
            } else {
                return value
            }
        }
    }
}
